# We use gsynth 1.2.1, released on August 6, 2021.

# package
require("readr")
require("dplyr")
require("tidyr")
require("ggplot2")
require("gsynth")
require("panelView")
theme_set(theme_bw(base_size = 12))

# set working directory
setwd()

# load
votematch <- read_csv(
  "2022VOTEMATCH20220812.csv", locale = locale(encoding = "SJIS")
)
votematch$DATE <- as.POSIXct(as.character(votematch$DATE), format = "%Y-%m-%d %H:%M:%S", tz ='Japan')
votematch <- votematch[votematch$DATE <= as.POSIXct("2022-07-10 20:00:00", tz="Japan"),]
votematch <- votematch[!is.na(votematch$DATE) & votematch$AGE != 1,]

# time
votematch$TIME <- NA
for (i in 1:18) {
  votematch$TIME[votematch$DATE >= as.POSIXct("2022-06-23 00:00:00", tz="Japan") + (i-1)*86400 & 
                 votematch$DATE < as.POSIXct("2022-06-23 00:00:00", tz="Japan") + i*86400] <- i
}
table(votematch$TIME)

# aggregate values by prefecture
votematch.prefecture <- votematch %>% 
  mutate(LDP = if_else(PARTY == 1, 1, 0), 
         Q18 = recode(Q18, `1` = 5, `2` = 4, `3` = 3, 
                      `4` = 2, `5` = 1, `99` = NA_real_), 
         MALE = if_else(SEX == 1, 1, 0), 
         FEMALE = if_else(SEX == 2, 1, 0), 
         AGE20 = if_else(AGE %in% c(2, 3), 1, 0), 
         AGE30 = if_else(AGE == 4, 1, 0), 
         AGE40 = if_else(AGE == 5, 1, 0), 
         AGE50 = if_else(AGE == 6, 1, 0), 
         AGE60 = if_else(AGE == 7, 1, 0), 
         AGE70 = if_else(AGE %in% c(8, 9), 1, 0)) %>% 
  group_by(PREFECTURE, TIME) %>% 
  summarise(LDP = mean(LDP, na.rm = TRUE), 
            Q18 = mean(Q18, na.rm = TRUE), 
            SIZE = n(), 
            MALE = mean(MALE, na.rm = TRUE), 
            FEMALE = mean(FEMALE, na.rm = TRUE), 
            AGE20 = mean(AGE20, na.rm = TRUE), 
            AGE30 = mean(AGE30, na.rm = TRUE), 
            AGE40 = mean(AGE40, na.rm = TRUE), 
            AGE50 = mean(AGE50, na.rm = TRUE), 
            AGE60 = mean(AGE60, na.rm = TRUE), 
            AGE70 = mean(AGE70, na.rm = TRUE)) %>% 
  ungroup()

# Abe's supporting speech
votematch.prefecture$ABE <- 0
## 大家敏志 (Fukuoka) / June 24
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 40 & 
                         votematch.prefecture$TIME >= 2] <- 1
## 江島潔 (Yamaguchi) / June 24
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 35 & 
                         votematch.prefecture$TIME >= 2] <- 1
## 末松信介 (Hyogo) / June 26
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 28 & 
                         votematch.prefecture$TIME >= 4] <- 1
## 山崎正昭 (Fukui) / June 27
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 18 & 
                         votematch.prefecture$TIME >= 5] <- 1
## 岡田直樹 (Ishikawa) / June 27
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 17 & 
                         votematch.prefecture$TIME >= 5] <- 1
## 生稲晃子 (Tokyo) / June 27
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 13 & 
                         votematch.prefecture$TIME >= 5] <- 1
## 松川るい (Osaka) / June 28
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 27 & 
                         votematch.prefecture$TIME >= 6] <- 1
## 佐藤啓 (Nara) / June 28
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 29 & 
                         votematch.prefecture$TIME >= 6] <- 1
## 古庄玄知 (Oita) / June 29
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 44 & 
                         votematch.prefecture$TIME >= 7] <- 1
## 山本順三 (Ehime) / June 30
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 38 & 
                         votematch.prefecture$TIME >= 8] <- 1
## 山本佐知子 (Mie) / July 1
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 24 & 
                         votematch.prefecture$TIME >= 9] <- 1
## 長谷川岳 (Hokkaido) / July 2
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 1 & 
                         votematch.prefecture$TIME >= 10] <- 1
## 臼井正一 & 猪口邦子 (Chiba) / July 3
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 12 & 
                         votematch.prefecture$TIME >= 11] <- 1
## 桜井充 (Miyagi) / July 5
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 4 & 
                         votematch.prefecture$TIME >= 13] <- 1
## 星北斗 (Fukushima) / July 6
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 7 & 
                         votematch.prefecture$TIME >= 14] <- 1
## 浅尾慶一郎 (Kanagawa) / July 6
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 14 & 
                         votematch.prefecture$TIME >= 14] <- 1
## 小野田紀美 (Okayama) / July 7
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 33 & 
                         votematch.prefecture$TIME >= 15] <- 1

# treatment variable
votematch.prefecture$TREATMENT <- 0
votematch.prefecture$TREATMENT[votematch.prefecture$TIME %in% c(16, 17, 18) & 
                               votematch.prefecture$PREFECTURE %in% c(29, 35)] <- 1

# visualizing data
panelview(LDP ~ TREATMENT, data = votematch.prefecture, 
          index = c("PREFECTURE","TIME"), pre.post = TRUE)
panelview(LDP ~ TREATMENT, data = votematch.prefecture, 
          index = c("PREFECTURE","TIME"), type = "outcome")
panelview(Q18 ~ TREATMENT, data = votematch.prefecture, 
          index = c("PREFECTURE","TIME"), pre.post = TRUE)
panelview(Q18 ~ TREATMENT, data = votematch.prefecture, 
          index = c("PREFECTURE","TIME"), type = "outcome")

# generalized synthetic control
## support for LDP
## Nara
pre.treatment <- nrow(votematch.prefecture[votematch.prefecture$PREFECTURE == 29 & 
                                           votematch.prefecture$TREATMENT == 0 & 
                                           votematch.prefecture$SIZE >= 50,])
gsynth.nara1 <- gsynth(
  LDP ~ TREATMENT + ABE + SIZE + MALE + FEMALE + 
    AGE20 + AGE30 + AGE40 + AGE50 + AGE60 + AGE70, 
  data = votematch.prefecture[votematch.prefecture$PREFECTURE != 35 & 
                              votematch.prefecture$SIZE >= 50,], 
  index = c("PREFECTURE","TIME"), 
  se = TRUE, inference = "parametric", 
  parallel = TRUE, min.T0 = pre.treatment, 
  r = c(0, pre.treatment-2), CV = TRUE, force = "two-way", 
  nboots = 1000, seed = 12345
)
print(gsynth.nara1)
plot(gsynth.nara1, type = "gap", id = 29, main = "Nara")
gsynth.nara1.plot <- plot(
  gsynth.nara1, type = "counterfactual", id = 29, 
  raw = "all", main = "Nara", xlab = "Date") + 
  scale_color_manual(breaks = c("tr", "raw.co", "ct"), 
                     values = c("tr" = "black", 
                                "ct" = "black", 
                                "raw.co" = "gray90"), 
                     labels = c("tr" = "Treated", 
                                "ct" = "Estimated Y(0)", 
                                "raw.co" = "Controls")) + 
  scale_linetype_manual(breaks = c("tr", "raw.co", "ct"), 
                        values = c("tr" = "solid", 
                                   "ct" = "dashed", 
                                   "raw.co" = "solid"), 
                        labels = c("tr" = "Treated", 
                                   "ct" = "Estimated Y(0)", 
                                   "raw.co" = "Controls")) + 
  scale_size_manual(breaks = c("tr", "raw.co", "ct"), 
                    values = c("tr" = 0.6, "ct" = 0.6, "raw.co" = 0.5), 
                    labels = c("tr" = "Treated", 
                               "ct" = "Estimated Y(0)", 
                               "raw.co" = "Controls")) + 
  scale_x_continuous(breaks = c(5, 10, 15), 
                     labels = c("June 27", "July 2", "July 7"))
plot(gsynth.nara1.plot)
## Yamaguchi
pre.treatment <- nrow(votematch.prefecture[votematch.prefecture$PREFECTURE == 35 & 
                                           votematch.prefecture$TREATMENT == 0 & 
                                           votematch.prefecture$SIZE >= 50,])
gsynth.yamaguchi1 <- gsynth(
  LDP ~ TREATMENT + ABE + SIZE + MALE + FEMALE + 
    AGE20 + AGE30 + AGE40 + AGE50 + AGE60 + AGE70, 
  data = votematch.prefecture[votematch.prefecture$PREFECTURE != 29 & 
                              votematch.prefecture$SIZE >= 50,], 
  index = c("PREFECTURE","TIME"), 
  se = TRUE, inference = "parametric", 
  parallel = TRUE, min.T0 = pre.treatment, 
  r = c(0, pre.treatment-2), CV = TRUE, force = "two-way", 
  nboots = 1000, seed = 12345
)
print(gsynth.yamaguchi1)
plot(gsynth.yamaguchi1, type = "gap", id = 35, main = "Yamaguchi")
gsynth.yamaguchi1.plot <- plot(
  gsynth.yamaguchi1, type = "counterfactual", id = 35, 
  raw = "all", main = "Yamaguchi", xlab = "Date") + 
  scale_color_manual(breaks = c("tr", "raw.co", "ct"), 
                     values = c("tr" = "black", 
                                "ct" = "black", 
                                "raw.co" = "gray90"), 
                     labels = c("tr" = "Treated", 
                                "ct" = "Estimated Y(0)", 
                                "raw.co" = "Controls")) + 
  scale_linetype_manual(breaks = c("tr", "raw.co", "ct"), 
                        values = c("tr" = "solid", 
                                   "ct" = "dashed", 
                                   "raw.co" = "solid"), 
                        labels = c("tr" = "Treated", 
                                   "ct" = "Estimated Y(0)", 
                                   "raw.co" = "Controls")) + 
  scale_size_manual(breaks = c("tr", "raw.co", "ct"), 
                    values = c("tr" = 0.6, "ct" = 0.6, "raw.co" = 0.5), 
                    labels = c("tr" = "Treated", 
                               "ct" = "Estimated Y(0)", 
                               "raw.co" = "Controls")) + 
  scale_x_continuous(breaks = c(5, 10, 15), 
                     labels = c("June 27", "July 2", "July 7"))
plot(gsynth.yamaguchi1.plot)

## restricting rights to public safety
## Nara
pre.treatment <- nrow(votematch.prefecture[votematch.prefecture$PREFECTURE == 29 & 
                                           votematch.prefecture$TREATMENT == 0 & 
                                           votematch.prefecture$SIZE >= 50,])
gsynth.nara2 <- gsynth(
  Q18 ~ TREATMENT + ABE + SIZE + MALE + FEMALE + 
    AGE20 + AGE30 + AGE40 + AGE50 + AGE60 + AGE70, 
  data = votematch.prefecture[votematch.prefecture$PREFECTURE != 35 & 
                              votematch.prefecture$SIZE >= 50,], 
  index = c("PREFECTURE","TIME"), 
  se = TRUE, inference = "parametric", 
  parallel = TRUE, min.T0 = pre.treatment, 
  r = c(0, pre.treatment-2), CV = TRUE, force = "two-way", 
  nboots = 1000, seed = 12345
)
print(gsynth.nara2)
plot(gsynth.nara2, type = "gap", id = 29, main = "Nara")
gsynth.nara2.plot <- plot(
  gsynth.nara2, type = "counterfactual", id = 29, 
  raw = "all", main = "Nara", xlab = "Date") + 
  scale_color_manual(breaks = c("tr", "raw.co", "ct"), 
                     values = c("tr" = "black", 
                                "ct" = "black", 
                                "raw.co" = "gray90"), 
                     labels = c("tr" = "Treated", 
                                "ct" = "Estimated Y(0)", 
                                "raw.co" = "Controls")) + 
  scale_linetype_manual(breaks = c("tr", "raw.co", "ct"), 
                        values = c("tr" = "solid", 
                                   "ct" = "dashed", 
                                   "raw.co" = "solid"), 
                        labels = c("tr" = "Treated", 
                                   "ct" = "Estimated Y(0)", 
                                   "raw.co" = "Controls")) + 
  scale_size_manual(breaks = c("tr", "raw.co", "ct"), 
                    values = c("tr" = 0.6, "ct" = 0.6, "raw.co" = 0.5), 
                    labels = c("tr" = "Treated", 
                               "ct" = "Estimated Y(0)", 
                               "raw.co" = "Controls")) + 
  scale_x_continuous(breaks = c(5, 10, 15), 
                     labels = c("June 27", "July 2", "July 7"))
plot(gsynth.nara2.plot)
## Yamaguchi
pre.treatment <- nrow(votematch.prefecture[votematch.prefecture$PREFECTURE == 35 & 
                                           votematch.prefecture$TREATMENT == 0 & 
                                           votematch.prefecture$SIZE >= 50,])
gsynth.yamaguchi2 <- gsynth(
  Q18 ~ TREATMENT + ABE + SIZE + MALE + FEMALE + 
    AGE20 + AGE30 + AGE40 + AGE50 + AGE60 + AGE70, 
  data = votematch.prefecture[votematch.prefecture$PREFECTURE != 29 & 
                              votematch.prefecture$SIZE >= 50,], 
  index = c("PREFECTURE","TIME"), 
  se = TRUE, inference = "parametric", 
  parallel = TRUE, min.T0 = pre.treatment, 
  r = c(0, pre.treatment-2), CV = TRUE, force = "two-way", 
  nboots = 1000, seed = 12345
)
print(gsynth.yamaguchi2)
plot(gsynth.yamaguchi2, type = "gap", id = 35, main = "Yamaguchi")
gsynth.yamaguchi2.plot <- plot(
  gsynth.yamaguchi2, type = "counterfactual", id = 35, 
  raw = "all", main = "Yamaguchi", xlab = "Date") + 
  scale_color_manual(breaks = c("tr", "raw.co", "ct"), 
                     values = c("tr" = "black", 
                                "ct" = "black", 
                                "raw.co" = "gray90"), 
                     labels = c("tr" = "Treated", 
                                "ct" = "Estimated Y(0)", 
                                "raw.co" = "Controls")) + 
  scale_linetype_manual(breaks = c("tr", "raw.co", "ct"), 
                        values = c("tr" = "solid", 
                                   "ct" = "dashed", 
                                   "raw.co" = "solid"), 
                        labels = c("tr" = "Treated", 
                                   "ct" = "Estimated Y(0)", 
                                   "raw.co" = "Controls")) + 
  scale_size_manual(breaks = c("tr", "raw.co", "ct"), 
                    values = c("tr" = 0.6, "ct" = 0.6, "raw.co" = 0.5), 
                    labels = c("tr" = "Treated", 
                               "ct" = "Estimated Y(0)", 
                               "raw.co" = "Controls")) + 
  scale_x_continuous(breaks = c(5, 10, 15), 
                     labels = c("June 27", "July 2", "July 7"))
plot(gsynth.yamaguchi2.plot)

# placebo test
placebo.test.ldp <- data.frame(
  PREFECTURE = rep(NA, 47), ATT = rep(NA, 47)
)
for (i in 1:47) {
  votematch.prefecture$PLACEBO <- 0
  votematch.prefecture$PLACEBO[votematch.prefecture$PREFECTURE == i & 
                               votematch.prefecture$TIME %in% c(16, 17, 18)] <- 1
  pre.treatment <- nrow(votematch.prefecture[votematch.prefecture$PREFECTURE == i & 
                                             votematch.prefecture$PLACEBO == 0 & 
                                             votematch.prefecture$SIZE >= 50,])
  gsynth.placebo <- try(gsynth(
    LDP ~ PLACEBO + ABE + SIZE + MALE + FEMALE + 
      AGE20 + AGE30 + AGE40 + AGE50 + AGE60 + AGE70, 
    data = votematch.prefecture[!(votematch.prefecture$PREFECTURE %in% c(29, 35)) & 
                                votematch.prefecture$SIZE >= 50,], 
    index = c("PREFECTURE","TIME"), 
    se = TRUE, inference = "parametric", 
    parallel = TRUE, min.T0 = pre.treatment, 
    r = c(0, pre.treatment-2), CV = TRUE, force = "two-way", 
    nboots = 1000, seed = 12345
  ))
  if (class(gsynth.placebo) == "try-error") {
    placebo.test.ldp$PREFECTURE[i] <- i
    placebo.test.ldp$ATT[i] <- NA
  }
  if (class(gsynth.placebo) != "try-error") {
    placebo.test.ldp$PREFECTURE[i] <- i
    placebo.test.ldp$ATT[i] <- gsynth.placebo$att.avg
  }
}
## save
write.csv(placebo.test.ldp, "placebo_test.csv", 
          row.names = FALSE, na = "", fileEncoding = "UTF8")
## graph
placebo.test.ldp.plot <- ggplot(placebo.test.ldp, aes(x = ATT)) + 
  geom_histogram(color = "white") + 
  geom_hline(aes(yintercept = 0), color = "gray90") + 
  annotate("segment", color = "black", 
           x = gsynth.yamaguchi1$att.avg, 
           xend = gsynth.yamaguchi1$att.avg,
           y = 1.0, yend = 0.0, 
           arrow = arrow(type = "closed", length = unit(0.1, "inches"))) + 
  annotate("text", x = 0.058, y = 1.2, parse = TRUE, 
           colour = "black", label = "Yamaguchi") + 
  scale_x_continuous(breaks = c(-0.03, 0, 0.03, 0.06)) + 
  labs(x = "Average Placebo Effect", y = "Frequency")
plot(placebo.test.ldp.plot)

# generalized synthetic control (full sample)
## support for LDP
pre.treatment <- nrow(votematch.prefecture[votematch.prefecture$PREFECTURE == 35 & 
                                           votematch.prefecture$TREATMENT == 0,])
gsynth.yamaguchi3 <- gsynth(
  LDP ~ TREATMENT + ABE + SIZE + MALE + FEMALE + 
    AGE20 + AGE30 + AGE40 + AGE50 + AGE60 + AGE70, 
  data = votematch.prefecture[votematch.prefecture$PREFECTURE != 29,], 
  index = c("PREFECTURE","TIME"), 
  se = TRUE, inference = "parametric", 
  parallel = TRUE, min.T0 = pre.treatment, 
  r = c(0, pre.treatment-2), CV = TRUE, force = "two-way", 
  nboots = 1000, seed = 12345
)
print(gsynth.yamaguchi3)
plot(gsynth.yamaguchi3, type = "gap", id = 35, main = "Yamaguchi")
gsynth.yamaguchi3.plot <- plot(
  gsynth.yamaguchi3, type = "counterfactual", id = 35, 
  raw = "all", main = "Yamaguchi", xlab = "Date") + 
  scale_color_manual(breaks = c("tr", "raw.co", "ct"), 
                     values = c("tr" = "black", 
                                "ct" = "black", 
                                "raw.co" = "gray90"), 
                     labels = c("tr" = "Treated", 
                                "ct" = "Estimated Y(0)", 
                                "raw.co" = "Controls")) + 
  scale_linetype_manual(breaks = c("tr", "raw.co", "ct"), 
                        values = c("tr" = "solid", 
                                   "ct" = "dashed", 
                                   "raw.co" = "solid"), 
                        labels = c("tr" = "Treated", 
                                   "ct" = "Estimated Y(0)", 
                                   "raw.co" = "Controls")) + 
  scale_size_manual(breaks = c("tr", "raw.co", "ct"), 
                    values = c("tr" = 0.6, "ct" = 0.6, "raw.co" = 0.5), 
                    labels = c("tr" = "Treated", 
                               "ct" = "Estimated Y(0)", 
                               "raw.co" = "Controls")) + 
  scale_x_continuous(breaks = c(5, 10, 15), 
                     labels = c("June 27", "July 2", "July 7"))
plot(gsynth.yamaguchi3.plot)
## restricting rights to public safety
pre.treatment <- nrow(votematch.prefecture[votematch.prefecture$PREFECTURE == 35 & 
                                           votematch.prefecture$TREATMENT == 0,])
gsynth.yamaguchi4 <- gsynth(
  Q18 ~ TREATMENT + ABE + SIZE + MALE + FEMALE + 
    AGE20 + AGE30 + AGE40 + AGE50 + AGE60 + AGE70, 
  data = votematch.prefecture[votematch.prefecture$PREFECTURE != 29,], 
  index = c("PREFECTURE","TIME"), 
  se = TRUE, inference = "parametric", 
  parallel = TRUE, min.T0 = pre.treatment, 
  r = c(0, pre.treatment-2), CV = TRUE, force = "two-way", 
  nboots = 1000, seed = 12345
)
print(gsynth.yamaguchi4)
plot(gsynth.yamaguchi4, type = "gap", id = 35, main = "Yamaguchi")
gsynth.yamaguchi4.plot <- plot(
  gsynth.yamaguchi4, type = "counterfactual", id = 35, 
  raw = "all", main = "Yamaguchi", xlab = "Date") + 
  scale_color_manual(breaks = c("tr", "raw.co", "ct"), 
                     values = c("tr" = "black", 
                                "ct" = "black", 
                                "raw.co" = "gray90"), 
                     labels = c("tr" = "Treated", 
                                "ct" = "Estimated Y(0)", 
                                "raw.co" = "Controls")) + 
  scale_linetype_manual(breaks = c("tr", "raw.co", "ct"), 
                        values = c("tr" = "solid", 
                                   "ct" = "dashed", 
                                   "raw.co" = "solid"), 
                        labels = c("tr" = "Treated", 
                                   "ct" = "Estimated Y(0)", 
                                   "raw.co" = "Controls")) + 
  scale_size_manual(breaks = c("tr", "raw.co", "ct"), 
                    values = c("tr" = 0.6, "ct" = 0.6, "raw.co" = 0.5), 
                    labels = c("tr" = "Treated", 
                               "ct" = "Estimated Y(0)", 
                               "raw.co" = "Controls")) + 
  scale_x_continuous(breaks = c(5, 10, 15), 
                     labels = c("June 27", "July 2", "July 7"))
plot(gsynth.yamaguchi4.plot)
